<?php

namespace App\V3\V3_2_7\Controllers;

use App\V3\V3_2_7\Models\SystemConfigModel;
use Illuminate\Http\Request;
use App\V3\V3_2_7\Models\AdminModel;
use OSS\OssClient;
use App\V3\V3_2_7\Models\AdminAddendumModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\V3\V3_2_7\Services\AdminService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Carbon;
use App\V3\V3_2_7\Services\MaterielTypeService;
use App\V3\V3_2_7\Services\UserManageService;
use App\Rules\JsonArray;
use Illuminate\Validation\Rule;
use App\V3\V3_2_7\Models\AuditImagesModel;
class SystemSettingsController extends BaseController
{
    protected $adminInfo;
    public function __construct(Request $request){
        parent::__construct($request);
        $this->adminInfo = AdminModel::select('id')
        ->where([
            'id'=>$this->getMainUserId(),
            'delete_flag'=>0,
            'admin_role'=>1,
            ['parent_id', '=', 0]
        ])->first();
        if(empty($this->adminInfo)){
            exit(json_encode([
                'code' => 1,
                'msg' => '您不是超级管理员，无法操作！',
                'data' => '',
            ]));
        }
    }
    /**
     * 用户列表
     * @param Request $request
     */
    public function getUserList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);
        
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return fail('', $info);
        }
        $list = [];
        if(!empty($this->adminInfo)){
            $list = AdminService::getUserList($data);
            if(!empty($list['data'])){
                foreach ($list['data'] as &$val){
                    if(!empty($val['vip_end_date'])){
                        $val['vip_end_date'] = Carbon::createFromTimestamp($val['vip_end_date'])->format('Y-m-d H:i:s');
                    }else{
                        $val['vip_end_date'] = 0;
                    }
                    if(!empty($val['storage_end_date'])){
                        $val['storage_end_date'] = Carbon::createFromTimestamp($val['storage_end_date'])->format('Y-m-d H:i:s');
                    }else{
                        $val['storage_end_date'] = 0;
                    }
                    
                    if(!empty($val['account_storage'])){
                       
                        $val['account_storage'] = $val['account_storage']/1024/1024;
                        
                    }else{
                        $val['account_storage'] = 0;
                    }
                }
                $list['reg_total_num'] = AdminModel::select(
                    DB::raw('count(*) as reg_num')
                    )->where([
                        'delete_flag' => 0,
                        'is_active'=>1,
                        ['parent_id','=',0],
                    ])->first()['reg_num']??0;
                $list['vip_total_num'] = AdminModel::select(
                    DB::raw('count(*) as vip_num')
                    )->from('admin as a')
                    ->leftJoin('admin_addendum as b', 'b.main_id', '=', 'a.id')
                    ->where([
                        'delete_flag' => 0,
                        'is_active'=>1,
                        ['parent_id','=',0],
                        'b.is_vip'=>1
                    ])->first()['vip_num']??0;
            }
        }
        return success($list);
    }
    
    /**
     * 用户管理列表
     * @param Request $request
     */
    public function getUserManageList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);
        
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return fail('', $info);
        }
        $list = [];
        if(!empty($this->adminInfo)){
            $list = AdminService::getUserManageList($data);
        }
        return success($list);
    }
    
    /**
     * 设置用户解封和禁止
     * @param Request $request
     */
    public function setUserDeblockingForbid(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' => 'required|integer',//用户id
            'is_active'=>[
                'required',
                'integer',
                Rule::in([0,1])
            ]
        ],[
            'id.required' => '参数用户`id`缺失',
            'id.integer' => '参数`id`仅能为数字',
            'is_active.required' => '参数禁用标示`is_active`缺失',
            'is_active.integer' => '参数禁用标示`is_active`仅能为数字',
            'is_active.In'=>'参数禁用标示`is_active`在0或1之间'
        ]);
        
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return fail('', $info);
        }
        $list = [];
        if(!empty($this->adminInfo)){
            return UserManageService::setUserDeblockingForbid($data);
        }
    }
    
    /**
     * 用户上传商品图片列表
     * @param Request $request
     */
    public function getImagesList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);
        
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return fail('', $info);
        }
        if(isset($data['start_time'])) {
            $timeValidator = Validator::make($data, [
                'start_time' => 'date_format:Y-m-d',//页面
            ],[
                'start_time.date_format' => '开始时间格式错误，例：'.date('Y-m-d')
            ]);
            
            //参数错误响应
            if ($infos = $timeValidator->errors()->first()) {
                return fail('', $infos);
            }
        }
        
        if(isset($data['end_time'])) {
            $timeValidator = Validator::make($data, [
                'end_time' => 'date_format:Y-m-d',//页面
            ],[
                'end_time.date_format' => '结束时间格式错误，例：'.date('Y-m-d')
            ]);
            
            //参数错误响应
            if ($infos = $timeValidator->errors()->first()) {
                return fail('', $infos);
            }
        }
        $list = [];
       
        if(!empty($this->adminInfo)){
            return success(MaterielTypeService::getImagesList($data));
        }
    }
    
    /**
     * 审核图片通过
     * @param Request $request
     */
    public function auditImagesPass(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(),
            [
                'ids' => 'required',//图片id(英文逗号分隔)
            ],[
                'ids.required' => '参数图片`id`缺失'
            ]);
        //参数错误响应
        if($info = $validator->errors()->first()){
            return fail('',$info);
        }
        $data['main_id'] = $this->getMainUserId();
        $this->log('审核图片通过', $data);
        if(!empty($this->adminInfo)){
            $bool = AuditImagesModel::pass($data);
            if($bool){
                return success();
            }else{
                return fail('','图片通过异常！');
            }
        }
        
        
    }
    
    /**
     * 设置标记用户
     * @param Request $request
     */
    public function setLabelUser(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(),
            [
                'user_info' => [
                    'required',
                    'json',
                    new JsonArray([
                        'main_id' => 'required|integer',//主账号id
                        'goods_id' => 'required|integer',//商品id
                        'img_path' => 'required',//违规图片路径
                    ])
                ],[
                    'log_info.required' => '参数`log_info`缺失',
                    'log_info.json' => '参数`log_info`仅能为json',
                  
                ]
            ]);
        //参数错误响应
        if($info = $validator->errors()->first()){
            return fail('',$info);
        }
        $data['main_id'] = $this->getMainUserId();
        $this->log('设置标示用户', $data);
        if(!empty($this->adminInfo)){
            $bool = UserManageService::setMarkUser($data);
            if($bool){
                return success();
            }else{
                return fail('','标示用户异常');
            }
        }
    }
    
    
    /**
     * 取消标记用户
     * @param Request $request
     */
    public function delLabelUser(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(),
            [
                'ids' => 'required',//标示用户id(英文逗号分隔)
            ],[
                'ids.required' => '参数标示用户`id`缺失'
            ]);
        //参数错误响应
        if($info = $validator->errors()->first()){
            return fail('',$info);
        }
        $data['main_id'] = $this->getMainUserId();
        $this->log('取消标示用户', $data);
        if(!empty($this->adminInfo)){
            $bool = UserManageService::del($data);
            if($bool){
                return success();
            }else{
                return fail('','取消失败');
            }
        }
    }
    
    
    /**
     * 查看设置标记用户列表
     * @param Request $request
     */
    public function getLabelUserList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);
        
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return fail('', $info);
        }
        if(!empty($this->adminInfo)){
            return success(UserManageService::getLabelUserList($data));
        }
    }
    
    /**
     * 违规用户商品图片列表
     * @param Request $request
     */
    public function getIllegalGoodsImgList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' => 'required|integer',//用户id
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'id.required' => '参数用户`id`缺失',
            'id.integer' => '参数id`id`仅能为数字',
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);
        
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return fail('', $info);
        }
        $list = [];
        if(!empty($this->adminInfo)){
            return UserManageService::getIllegalGoodsImgList($data);
        }
    }
}
